package no.ntnu.item.ttm4115.termproject.discussionagent.gui;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.discussionagent.gui.Discussion;



/**
* This class is the dialog object which is used when inseting or editing comments on the
* discussion thread
* @author surya
*/
public class DiscussionDialog extends javax.swing.JDialog {


	private static final long serialVersionUID = 1L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel jLabelDiscussionText;
	private JTextArea jTextAreaDiscussionText;
	private AbstractAction abstractActionDiscussionDialogOK;
	private JButton jButtonDiscussionOK;
	
	@SuppressWarnings("unused")
	private DiscussionAgentGUI discussionAgentGUI;
	
	private boolean newComment;

	/**
	* main method to display this JDialog....
	* This is just for testing...your bundle will never call this method
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				//JFrame frame = new JFrame();
				JPanel panel = new JPanel();
				DiscussionDialog inst = new DiscussionDialog(panel, null, true);
				inst.setVisible(true);
			}
		});
	}
	
	/**
	 * Dialog Constructor
	 * @param frame
	 * @param tree
	 */
	public DiscussionDialog(JPanel panel, JTree tree, boolean newComment) {
		//super(frame);
		this.newComment = newComment;
		this.discussionAgentGUI = (DiscussionAgentGUI)panel;
		initGUI();
	}
	
	/**
	 * Method for initializing the dialog GUI
	 * You don't need to edit this method
	 */
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			{
				jLabelDiscussionText = new JLabel();
				jLabelDiscussionText.setText("Create new topic or Insert your comment...");
				jLabelDiscussionText.setFont(new java.awt.Font("Segoe UI",1,12));
			}
			{
				jButtonDiscussionOK = new JButton();
				jButtonDiscussionOK.setText("OK");
				jButtonDiscussionOK.setAction(getAbstractActionDiscussionDialogOK());
			}
			{
				jTextAreaDiscussionText = new JTextArea();
				jTextAreaDiscussionText.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(19, Short.MAX_VALUE)
				.addComponent(jLabelDiscussionText, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jTextAreaDiscussionText, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addComponent(jButtonDiscussionOK, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(44, 44));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(75, 75)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(jLabelDiscussionText, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
				    .addGroup(thisLayout.createSequentialGroup()
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addGroup(thisLayout.createParallelGroup()
				            .addComponent(jTextAreaDiscussionText, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE)
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(jButtonDiscussionOK, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
				                .addGap(194)))))
				.addContainerGap(51, 51));
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Method for getting the JTextArea component in the dialog GUI
	 * @return
	 */
	public JTextArea getJTextAreaDiscussionText() {
		return jTextAreaDiscussionText;
	}
	
	/**
	 * Method that deals when the OK button is clicked on the dialog GUI
	 * @return
	 */
	@SuppressWarnings("serial")
	public AbstractAction getAbstractActionDiscussionDialogOK() {
		if(abstractActionDiscussionDialogOK == null) {
			abstractActionDiscussionDialogOK = new AbstractAction("OK", null) {
				public void actionPerformed(ActionEvent evt) {
					System.out.println("okokok"+getJTextAreaDiscussionText().getText());
					//discussionAgentGUI.updateDiscussioTreeCommentAddedLocally(new Discussion(getJTextAreaDiscussionText().getText()));
					if (newComment){
						discussionAgentGUI.updateDiscussioTreeCommentAddedLocally(new Discussion(getJTextAreaDiscussionText().getText(), Long.toString(System.currentTimeMillis()), new Player(discussionAgentGUI.getPlayerName(),"","") ));
						}
						else{
							discussionAgentGUI.editDiscussioTreeCommentAddedLocally(getJTextAreaDiscussionText().getText());						
						}
					dispose();
				}
			};
		}
		return abstractActionDiscussionDialogOK;
	}

}
